package com.wocao.framework.config.mp;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * MP注入处理器
 *
 * @author Lion Li
 * @date 2021/4/25
 */
@Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            //根据属性名字设置要填充的值
            if (metaObject.hasGetter("createTime")) {
                this.setFieldValByName("createTime", new Date(), metaObject);
            }
            if (metaObject.hasGetter("createBy")) {
                this.setFieldValByName("createBy", "111", metaObject);
            }
        } catch (Exception e) {
            log.error("CreateAndUpdateMetaObjectHandler.insertFill is error,metaObject={}", metaObject, e);
        }
        updateFill(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            if (metaObject.hasGetter("updateBy")) {
                this.setFieldValByName("updateBy", "111", metaObject);
            }
            if (metaObject.hasGetter("updateTime")) {
                this.setFieldValByName("updateTime", new Date(), metaObject);
            }
        } catch (Exception e) {
            log.error("CreateAndUpdateMetaObjectHandler.updateFill is error,metaObject={}", metaObject, e);
        }
    }

}
